﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Collections.Specialized;
using System.Collections;

namespace Medianamik.UI
{
    public class MedianamikSiteMapNode : System.Web.SiteMapNode
    {
        private SiteMapProvider _provider;
        private string _title;
        private string _url;
        private string _key;
        private NameValueCollection _attributes;

        public MedianamikSiteMapNode(SiteMapProvider provider, string key, string url)
            : base(provider, key, url)
        {
            _provider = provider;
            _key = key;
            _url = url;
        }

        public MedianamikSiteMapNode(SiteMapProvider provider, string key, string url, string title, string description, IList roles, NameValueCollection attributes, NameValueCollection explicitResourceKeys, string implicitResourceKey)
            : base(provider, key, url, title, description, roles, attributes, explicitResourceKeys, implicitResourceKey)
        {
            _provider = provider;
            _key = key;
            _url = url;
            _title = title;
            _attributes = attributes;
        }

        [
        Localizable(true)
        ]
        public override string Description
        {
            get
            {
                return String.Empty;
            }
            set
            {
                throw new NotImplementedException("SiteMap description is not implemented in Medianamik.");
            }
        }

        // Access custom attributes. 
        public override string this[string key]
        {
            get
            {
                string text = null;
                if (_attributes != null)
                {
                    text = _attributes[key];
                }

                if (_provider.EnableLocalization)
                {
                    // Try the implicit resource first 
                    string localizedText = GetImplicitResourceString(key);
                    if (localizedText != null)
                    {
                        return localizedText;
                    }

                    // If not found, try the explicit resource.
                    localizedText = GetExplicitResourceString(key, text, false);
                    if (localizedText != null)
                    {
                        return localizedText;
                    }
                }

                return text;
            }

            set
            {
                if (ReadOnly)
                {
                    throw new InvalidOperationException("SiteMap is read-only.");
                }

                if (_attributes == null)
                {
                    _attributes = new NameValueCollection();
                }

                _attributes[key] = value;
            }
        }

        [
        Localizable(true)
        ]
        public override string Title
        {
            get
            {
                if (_provider.EnableLocalization)
                {
                    string localizedText = GetImplicitResourceString("title");
                    if (localizedText != null)
                    {
                        return localizedText;
                    }

                    localizedText = GetExplicitResourceString("title", _title, false);
                    if (localizedText != null)
                    {
                        return localizedText;
                    }
                }

                return _title == null ? String.Empty : _title;
            }
            set
            {
                if (ReadOnly)
                {
                    throw new InvalidOperationException("SiteMap is read-only.");
                }

                _title = value;
            }
        }

    }
}
